<?php
App::uses('AppController', 'Controller');
App::uses('Sanitize','Utility');
/**
 * Players Controller
 *
 * @property Player $Player
 */
class PlayersController extends AppController {
	public $components = array('RequestHandler');
	public $helpers = array('QuickSearch','Js');
	public $paginate = array(
			'limit' => 3
			);
	
/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Player->recursive = 0;
		$this->set('players', $this->paginate());
	}

/**
 * search method
 *
 * @return void
 */
	public function search() {
		if($this->data) {
			$data = Sanitize::clean($this->data);
			$this->Session->write('Player.search_data', $data);
		}
		else {
			$data = $this->Session->read('Player.search_data');
			$this->data = $data;
		}
		$conditions = $this->Player->buildConditions($data);
		$this->paginate = array(
				'limit' => 3,
				'order' => array('Player.id' => 'asc'),
				'conditions' => $conditions);
		$results = $this->paginate('Player');
		$this->set('players', $results);
	
		$this->render('index');
	}
	
/**
 * clear search method
 *
 * @return void
 */	
	public function clear() {
		$this->Session->delete('Player.search_data');
	
		$this->Player->recursive = 0;
		$this->set('players', $this->paginate());
	
		$this->render('index');
	}

/**
 * view method
 *
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->Player->id = $id;
		if (!$this->Player->exists()) {
			throw new NotFoundException(__('Invalid player'));
		}
		$this->set('player', $this->Player->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Player->create();
			if ($this->Player->save($this->request->data)) {
				$this->Session->setFlash(__('The player has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The player could not be saved. Please, try again.'));
			}
		}
	}

/**
 * edit method
 *
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Player->id = $id;
		if (!$this->Player->exists()) {
			throw new NotFoundException(__('Invalid player'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Player->save($this->request->data)) {
				$this->Session->setFlash(__('The player has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The player could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->Player->read(null, $id);
		}
	}

/**
 * delete method
 *
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Player->id = $id;
		if (!$this->Player->exists()) {
			throw new NotFoundException(__('Invalid player'));
		}
		if ($this->Player->delete()) {
			$this->Session->setFlash(__('Player deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Player was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
}
